package com.small.flowable.modules.app.services;

import com.small.flowable.modules.app.dto.rsp.TaskRsp;
import lombok.AllArgsConstructor;
import org.flowable.engine.HistoryService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.service.impl.TaskQueryProperty;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务
 * @author wangguochao
 */
@Service
@AllArgsConstructor
public class TaskInstanceService {


    private final TaskService taskService;
    private final HistoryService historyService;


    public List<TaskRsp>  getHistoryListByProcessInstanceId(String processInstanceId) {

        List<HistoricTaskInstance> taskList = historyService.createHistoricTaskInstanceQuery()
                .processInstanceId(processInstanceId)
                .finished()
                .list();

        List<TaskRsp> records = new ArrayList<>();

        taskList.forEach(obj -> {
            records.add(trainToTaskRsp(obj));
        });
        return records;
    }

    public List<TaskRsp> getTaskListByProcessInstanceId(String processInstanceId) {

        List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();

        List<TaskRsp> records = new ArrayList<>();

        taskList.forEach(obj -> {
            records.add(trainToTaskRsp(obj));
        });
        return records;
    }

    /**
     *  获取任务列表
     * @param assignee 受让人
     * @return 任务列表
     */
    public List<TaskRsp> getTaskList(String assignee) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        if (!StringUtils.isEmpty(assignee)) {
            taskQuery.taskCandidateOrAssigned(assignee);
        }
        List<Task> taskList = taskQuery.orderBy(TaskQueryProperty.CREATE_TIME).desc().list();
        List<TaskRsp> records = new ArrayList<>(taskList.size());
        taskList.forEach(obj -> {
            records.add(trainToTaskRsp(obj));
        });
        return records;
    }

    public TaskRsp trainToTaskRsp(TaskInfo task){
        return TaskRsp.builder()
                .processDefinitionId(task.getProcessDefinitionId())
                .taskDefinitionKey(task.getTaskDefinitionKey())
                .processInstanceId(task.getProcessInstanceId())
                .taskId(task.getId())
                .taskName(task.getName())
                .createTime(task.getCreateTime())
                .executionId(task.getExecutionId())
                .formKey(task.getFormKey())
                .dueDate(task.getDueDate())
                .claimTime(task.getClaimTime())
                .scopeType(task.getScopeType())
                .subScopeId(task.getSubScopeId())
                .scopeId(task.getScopeId())
                .tenantId(task.getTenantId())
                .taskLocalVariables(task.getTaskLocalVariables())
                .parentTaskId(task.getParentTaskId())
                .build();
    }


}
